<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item prop="id">
        <el-input
          v-model="queryParams.floorId"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="buildingId">
        <el-select v-model="queryParams.buildingId" placeholder="请选择楼宇" clearable>
          <el-option
            v-for="item in buildingOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable>
          <el-option
            v-for="item in nowUnitOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="purposeId">
        <el-select v-model="queryParams.purposeId" placeholder="请选择房间用途" clearable>
          <el-option
            v-for="dict in dict.type.community_rooms_purpose"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary"  size="mini" @click="handleAdd">新增</el-button>
        <el-button type="primary"  size="mini" :disabled="single" @click="handleConnectResident()">已关联住户</el-button>
        <el-button type="primary"  size="mini" :disabled="single" @click="handleUnConnectResident()">未关联住户</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="roomsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间号" align="center" prop="id" />
      <el-table-column label="楼宇名称" align="center" prop="buildingName" />
      <el-table-column label="单元名称" align="center" prop="unitName" />
      <el-table-column label="楼层" align="center" prop="floorName" />
      <el-table-column label="用途" align="center" prop="purposeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.community_rooms_purpose" :value="scope.row.purposeId"/>
        </template>
      </el-table-column>
      <el-table-column label="建筑面积" align="center" prop="area" />
      <el-table-column label="使用面积" align="center" prop="userArea" />
      <el-table-column label="住户数" align="center" prop="countResident">
        <!--        <el-link href="http://localhost/system/user" type="primary">{{countResident}}</el-link>-->
        <template slot-scope="scope" disabled="true">
<!--          <router-link :to="{name: 'Unit', params: {buildingId: scope.row.id} }" >{{countResident}}</router-link>-->
          <el-button type="text" @click="handleRoute(scope.row.id)" :disabled="scope.row.countResident === 0"> {{scope.row.countResident}} </el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:rooms:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:rooms:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add-and-update
      :visible.sync="auVisible"
      :form="form" :title="title"
      :getList="getList"
      :reset="reset"
    />
    <handle-resident
      :visible.sync="openConnectVisible"
      :title="title"
      :getList="getList"
      :reset="reset"
      :roomId.sync="selectResidentId"
      :unRoomId.sync="selectUnConnResidentId"
      :buttonShow.sync="buttonShow"
    />

  </div>
</template>

<script>
import { listRooms, getRooms, delRooms} from "@/api/system/rooms";
import addAndUpdate from "./addAndUpdate"
import { listBuilding } from "@/api/system/building";
import {listUnit} from "@/api/system/unit";
import handleResident from "./handleResident"
import {buildingList} from "../../../api/system/building";
import {unitList} from "../../../api/system/unit";
export default {
  name: "Rooms",
  dicts: [`community_rooms_purpose`],
  components:{ addAndUpdate , handleResident },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      selectId: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // rooms表格数据
      roomsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      auVisible: false,
      // 是否可以跳转
      routeDis: false,
      // 显示关联用户
      openConnectVisible: false,
      // 关联界面是否显示添加关联按钮
      buttonShow: false,

      selectResidentId: 0,
      selectUnConnResidentId:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        floorId: null,
        purposeId: null,
        area: null,
        userArea: null,
      },
      // 表单参数
      form: {},
      buildingOptions: [
        { label: "一号楼", value: 1},
        { label: "二号楼", value: 2}
      ],
      unitOptions: [
        { label: "一单元", value: 1},
        { label: "二单元", value: 2},
      ],
      countResident: 10,
      residentList: []
    };
  },
  created() {
    this.queryParams.floorId = this.$route.params.roomId
    this.getList();
    buildingList().then(res => {
      this.buildingOptions = res.rows
    })
    unitList().then(res => {
      this.unitOptions = res.rows
      console.log(this.unitOptions)
    })
  },
  computed:{
    nowUnitOptions(){
      return this.unitOptions.filter(item => this.queryParams.buildingId === item.buildingId)
    },
  },
  watch:{
    '$route.params.roomId'(n){
      this.initHandler(n)
    }
  },
  methods: {
    initHandler(id) {
      this.queryParams.floorId = id
      this.getList()
    },
    /** 查询rooms列表 */
    getList() {
      this.loading = true;
      listRooms(this.queryParams).then(response => {
        this.roomsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        floorId: null,
        purposeId: null,
        area: null,
        userArea: null,
        remark: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.selectId = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.auVisible = true;
      this.title = "添加房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRooms(id).then(response => {
        this.form = response.data;
        this.auVisible = true;
        this.title = "修改房间信息";
      });
    },
    /** 已关联用户按钮 */
    handleConnectResident(){
      this.title = "已关联用户"
      this.selectResidentId = this.selectId[0]
      this.openConnectVisible = true

    },
    /** 未关联用户 */
    handleUnConnectResident(){
      this.title = "未关联用户"
      this.selectUnConnResidentId = this.selectId[0]
      this.buttonShow = true
      this.openConnectVisible = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除employee编号为"' + ids + '"的数据项？').then(function() {
        return delRooms(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 路由跳转操作 */
    handleRoute(id){
      this.$router.push({name: 'Resident', params: { roomId: id}})
    }
  }
};
</script>
